# -*- coding: utf-8 -*-

from PyQt5 import uic

from PyQt5 import QtWidgets
from PyQt5.Qt import Qt


from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QMessageBox

# import matplotlib
# matplotlib.use('Qt5Agg')

from PyQt5.QtWidgets import QApplication

import constantValues as cv

import debugPrinter as dp

from PyQt5.QtCore import QSettings, QPoint, QSize

from PyQt5.QtWidgets import QInputDialog, QLineEdit
import os


qt_creator_file = os.path.join(os.path.dirname(__file__),"ui_config", "mainwindow.ui")
Ui_MainWindow, QtBaseClass = uic.loadUiType(qt_creator_file)

ini_file = os.path.join(os.path.dirname(__file__), "ui_config","uiSetting.ini")

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    # update_data = pyqtSignal(float,float)
    evt_win = pyqtSignal(str,str)
    # evt_win_data = pyqtSignal(str,int,str)

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.btn_flash_led.triggered.connect(self.flash_led_triggered)
        self.actionsetting.triggered.connect(self.setting_triggered)
        self.btn_start_recording.clicked.connect(self.start_recording_btn_click)
        self.btn_stop_recording.clicked.connect(self.stop_recording_btn_click)
        self.btn_mi_train.clicked.connect(self.mi_train_btn_click)
        self.btn_generate_model.clicked.connect(self.generate_model_btn_click)
        self.btn_mi_test.clicked.connect(self.mi_test_btn_click)

        self.settings = QSettings(ini_file, QSettings.IniFormat)     
        # Initial window size/pos last saved. Use default values for first time
        self.resize(self.settings.value("size", QSize(270, 225)))
        if (self.settings.value("pos") is not None) and (self.settings.value("size") is not None):
            screenRect = QApplication.desktop().screenGeometry()
            self.height = screenRect.height()
            if self.settings.value("pos").x()<(screenRect.width()-100) and \
                self.settings.value("pos").y()<(screenRect.height()-100):
                self.move(self.settings.value("pos", QPoint(50, 50)))            

        
    def log_info(self,s):
        if(s!=cv.LOG_INFO_INGNORE):
            self.textBrowser_evt_log.append(s)
            
    def setting_triggered(self):
        dp.dpt('setting_triggered - - -')
        self.evt_win.emit(cv.EVT_WIN_BTN_CLICK_SETTING,cv.DUMMY_STR)

    def flash_led_triggered(self):
        dp.dpt('flash_led_triggered - - -')
        self.evt_win.emit(cv.SERIAL_CMD_FALSH_LED,cv.DUMMY_STR)

    def keyPressEvent(self,event):
        if event.key()==Qt.Key_Q:
            self.close()

    def set_start_recording_btn_disable(self):
        self.btn_start_recording.setEnabled(False)

    def set_start_recording_btn_enable(self):
        self.btn_start_recording.setEnabled(True)

    def exit_app(self):
        self.close()

    def closeEvent(self, event):
        # do stuff
        r = QMessageBox.question(self,"Window Close","Are you sure to close?",
            QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes)
        if r == QMessageBox.Yes:
            self.evt_win.emit(cv.EVT_WIN_QUIT,cv.DUMMY_STR)        
            event.accept()

            self.settings.setValue("size", self.size())
            self.settings.setValue("pos", self.pos())

            QApplication.quit()
        else:
            event.ignore()
            
            
    def start_recording_btn_click(self):
        self.btn_start_recording.setEnabled(False)
        self.evt_win.emit(cv.EVT_WIN_BTN_START_RECORDING,cv.DUMMY_STR)
        self.log_info('START_RECORDING')

    def stop_recording_btn_click(self):
        self.evt_win.emit(cv.EVT_WIN_BTN_STOP_RECORDING,cv.DUMMY_STR)
        self.btn_start_recording.setEnabled(True)
        self.log_info('STOP_RECORDING')
        
    def mi_train_btn_click(self):
        self.evt_win.emit(cv.EVT_WIN_BTN_MI_TRAIN,cv.DUMMY_STR)
        self.log_info('EVT_WIN_BTN_MI_TRAIN')

    def generate_model_btn_click(self):
        self.evt_win.emit(cv.EVT_WIN_BTN_GENERATW_MODEL,cv.DUMMY_STR)
        self.log_info('This function not complete yet!')

    def mi_test_btn_click(self):
        self.evt_win.emit(cv.EVT_WIN_BTN_MI_TEST,cv.DUMMY_STR)
        self.log_info('This function not complete yet!')
        

    def show_error(self,s):
        QMessageBox.warning(self, "error", s)

    def get_input_fileName(self,hint):
        text, ok = QInputDialog().getText(self, "File Name For Saving",
                                 "(you can use Subject Name or Session name):", QLineEdit.Normal,hint)
        return text,ok
        # if ok and text:
        #     c = a[:a.rfind('/')+1] + text
        #     print(c)
        #     if (self.fs.make_path(c)):
        #         QMessageBox.warning(self, "error", "this file has been exist!")
        #         return 'f'
        #     else:
        #         self.fs.setup(c,text)
        #         return 's'

